package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.HashMap;

@Controller
@RequestMapping("/images")
public class ImageController {

    @Autowired
    private ServletContext ctx;

    @PostMapping
    @ResponseBody
    public Object updateImage(MultipartFile pic) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println(map);
        try {
            //通过服务器域对象获取相对项目路径
            String imagePath = UploadUtil.upload(pic, UploadUtil.IMG_PATH + "/upload");
            map.put("url", imagePath);
            map.put("status", 1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("url", "");
            map.put("status", 0);
        }
        return map;
    }


}
